package com.allwees.bs.module.pms.rest;

import com.allwees.bs.module.core.annotation.PrintLog;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.allwees.bs.core.modelbase.vo.R;
import com.allwees.core.common.constant.EStatus;
import com.allwees.support.marketing.domain.HotWord;
import com.allwees.support.marketing.dto.HotWordDto;
import com.allwees.support.marketing.service.HotWordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@PrintLog
@Slf4j
@RestController
@RequestMapping("/backstage/api/pms/hotword")
public class AdminHotWordApi{

    @Resource
    private HotWordService hotWordService;

    @GetMapping("/list")
    public R list(@RequestParam(name="current",required = false,defaultValue = "1") int pageNum,
                  @RequestParam(name="size",required = false,defaultValue = "20") int pageSize){
        return R.ok(hotWordService
                        .lambdaQuery()
                        .eq(HotWord::getStatus, EStatus.NORMAL.value())
                        .orderByDesc(HotWord::getSort)
                        .page(new Page<>(pageNum,pageSize)));
    }

    @PostMapping("/save")
    public R save(@Validated(HotWordDto.Add.class) @RequestBody HotWordDto hotWord){
        hotWordService.saveWord(hotWord);
        return R.ok();
    }

    @PostMapping("/edit")
    public R edit(@Validated(HotWordDto.Edit.class) @RequestBody HotWordDto hotWord){
        hotWordService.updateWord(hotWord);
        return R.ok();
    }

    @PostMapping("/enable")
    public R enable(@Validated(HotWordDto.Edit.class) @RequestBody HotWordDto hotWord){
        hotWordService.enable(hotWord.getId());
        return R.ok();
    }

    @PostMapping("/disable")
    public R disable(@Validated(HotWordDto.Edit.class) @RequestBody HotWordDto hotWord){
        hotWordService.disable(hotWord.getId());
        return R.ok();
    }
}
